package controllers

import (
	"NewEdu/models"
	"NewEdu/utils"
	"encoding/json"
	"strconv"
	"time"

	beego "github.com/beego/beego/v2/server/web"
)

// 用户跟进日志
type TUserLogController struct {
	beego.Controller
}

// Post ...
// @Title Post
// @Description 添加跟进记录
// @Param	sn	path	string	true	"学号"
// @Param	body	body	string 	models.TUserLog	true		"评论文字"
// @Success 201 {int} models.TUserLog
// @Failure 403 body is empty
// @router /addLog/:sn [post]
func (c *TUserLogController) AddLog() {

	sn := c.Ctx.Input.Param(":sn")
	var v models.TUserLog
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err != nil {
		c.Data["json"] = utils.Export(1001, "Unmarshal失败", err.Error())
		c.ServeJSON()
		return
	}
	v.UserSN = sn
	currentUser := c.Ctx.Input.GetData(models.CurrentUser).(*models.TUser)
	v.WriterSN = currentUser.Sn
	v.WriterName = currentUser.Name
	v.Creattime = time.Now()
	if _, err := models.AddTUserLog(&v); err == nil {
		c.Ctx.Output.SetStatus(201)
		c.Data["json"] = utils.Export(1000, "添加成功", v)
	} else {
		c.Data["json"] = utils.Export(1001, "添加失败", err.Error())
	}

	c.ServeJSON()
}

// GetAll ...
// @Title 获取用户跟进记录
// @Description 获取跟进记录
// @Param	sn	path	string	true	"学号"
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.TUserLog
// @Failure 403
// @router /query/:sn [get]
func (c *TUserLogController) GetAll() {

	var sortby []string = []string{"id"}
	var order []string = []string{"asc"}
	var query = make(map[string]string)
	var limit int64 = 10
	var offset int64
	sn := c.Ctx.Input.Param(":sn")
	// limit: 10 (default is 10)
	if v, err := c.GetInt64("limit"); err == nil {
		limit = v
	}
	// offset: 0 (default is 0)
	if v, err := c.GetInt64("offset"); err == nil {
		offset = v
	}
	if len(sn) > 0 {
		query["user_sn"] = sn
	}
	l, err := models.GetAllTUserLog(query, nil, sortby, order, offset, limit)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "查询失败", err.Error())
	} else {
		c.Data["json"] = utils.Export(1000, "查询成功", l)
	}
	c.ServeJSON()
}

// Put ...
// @Title Put
// @Description update the TUserLog
// @Param	logid		path 	string	true		"log 的id"
// @Param	body		body 	string	true		"body for TUserLog content"
// @Success 200 {object} models.TUserLog
// @Failure 403 :id is not int
// @router /appendLogComment/:logid [post]
func (c *TUserLogController) AppendLogComment() {
	idStr := c.Ctx.Input.Param(":logid")
	logid, err := strconv.Atoi(idStr)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "logid 不对", err.Error())
		c.ServeJSON()
		return
	}
	var log models.LogComment
	log.Content = string(c.Ctx.Input.RequestBody)
	log.CreatTime = time.Now().Unix()
	currentUser := c.Ctx.Input.GetData(models.CurrentUser).(*models.TUser)
	log.WriterSN = currentUser.Sn
	log.WriterName = currentUser.Name

	if err := models.AppendLogChild(logid, &log); err == nil {
		c.Data["json"] = utils.Export(1000, "ok", log)
	} else {
		c.Data["json"] = utils.Export(1001, "append失败", err.Error())
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the TUserLog
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *TUserLogController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	if err := models.DeleteTUserLog(id); err == nil {
		c.Data["json"] = utils.Export(1000, "ok", id)
	} else {
		c.Data["json"] = utils.Export(1001, "Delete失败", err.Error())
	}
	c.ServeJSON()
}
